package com.xhd.db;

import java.io.StringReader;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.util.XDate;

public class DBSQL {
	 private StringBuffer jm287;
	  private Object[] jm288;
	  private int[] jm289;
	  private int jm290;
	  private static final Log jm9;
	  static Class jm10;
	  static Class jm17;
	  static Class jm291;

	  static
	  {
	    Class tmp3_0 = jm10;
	    if (tmp3_0 == null)
	    {
	      
	      try
	      {
	         DBSQL.jm10 = Class.forName("com.sinocc.systemdbmanager.DBSQL");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp3_0);
	  }

	  public DBSQL()
	  {
	    this.jm287 = new StringBuffer(256);
	    this.jm289 = new int[8];
	    this.jm288 = new Object[8];
	    this.jm290 = 0;
	  }

	  public DBSQL(String paramString)
	  {
	    if (paramString != null)
	    {
	      this.jm287 = new StringBuffer(paramString.length() + 32);
	      this.jm287.append(paramString);
	    }
	    else
	    {
	      this.jm287 = new StringBuffer(128);
	    }
	    this.jm289 = new int[8];
	    this.jm288 = new Object[8];
	    this.jm290 = 0;
	  }

	  public void copy(DBSQL paramDBSQL)
	  {
	    setSQL(paramDBSQL.getSQL());
	    this.jm290 = paramDBSQL.jm290;
	    if (this.jm290 <= 0)
	      return;
	    this.jm289 = new int[this.jm290];
	    this.jm288 = new Object[this.jm290];
	    System.arraycopy(paramDBSQL.jm289, 0, this.jm289, 0, this.jm290);
	    System.arraycopy(paramDBSQL.jm288, 0, this.jm288, 0, this.jm290);
	  }

	  public int length()
	  {
	    return this.jm287.length();
	  }

	  public void setSQL(String paramString)
	  {
	    int i = this.jm287.length();
	    if (i > 0)
	      this.jm287.delete(0, i);
	    this.jm287.append(paramString);
	  }

	  public void appendSQL(String paramString)
	  {
	    this.jm287.append(paramString);
	  }

	  public void append(String paramString)
	  {
	    this.jm287.append(paramString);
	  }

	  public void append(int paramInt, DBWhereExp paramDBWhereExp)
	  {
	    if (paramDBWhereExp == null)
	      return;
	    paramDBWhereExp.toDBSQL(paramInt, this, true);
	  }

	  public String getSQL()
	  {
	    return this.jm287.toString();
	  }

	  public void clear()
	  {
	    if (this.jm287.length() > 0)
	      this.jm287.delete(0, this.jm287.length());
	    clearParameters();
	  }

	  public void clearParameters()
	  {
	    if (this.jm290 <= 0)
	      return;
	    for (int i = 0; i < this.jm290; ++i)
	    {
	      this.jm289[i] = 0;
	      this.jm288[i] = null;
	    }
	    this.jm290 = 0;
	  }

	  private void jm286(int paramInt1, int paramInt2, Object paramObject)
	  {
	    if (paramInt1 < 1)
	      throw new ArrayIndexOutOfBoundsException(paramInt1);
	    if (--paramInt1 >= this.jm289.length)
	    {
	      int i = this.jm289.length;
	      int[] arrayOfInt = new int[i + 4];
	      Object[] arrayOfObject = new Object[i + 4];
	      System.arraycopy(this.jm289, 0, arrayOfInt, 0, i);
	      System.arraycopy(this.jm288, 0, arrayOfObject, 0, i);
	      this.jm289 = null;
	      this.jm288 = null;
	      this.jm289 = arrayOfInt;
	      this.jm288 = arrayOfObject;
	    }
	    this.jm289[paramInt1] = paramInt2;
	    this.jm288[paramInt1] = paramObject;
	    this.jm290 += 1;
	  }

	  public void setNull(int paramInt1, int paramInt2)
	  {
	    jm286(paramInt1, paramInt2, null);
	  }

	  public void setInt(int paramInt1, int paramInt2)
	  {
	    jm286(paramInt1, 4, new Integer(paramInt2));
	  }

	  public void addInt(int paramInt)
	  {
	    jm286(this.jm290 + 1, 4, new Integer(paramInt));
	  }

	  public void setLong(int paramInt, long paramLong)
	  {
	    jm286(paramInt, 2, new Long(paramLong));
	  }

	  public void setString(int paramInt, String paramString)
	  {
	    jm286(paramInt, 12, paramString);
	  }

	  public void addString(String paramString)
	  {
	    jm286(this.jm290 + 1, 12, paramString);
	  }

	  public void setDate(int paramInt, java.util.Date paramDate)
	  {
	    if (paramDate == null)
	      jm286(paramInt, 93, null);
	    else
	      jm286(paramInt, 93, new Timestamp(paramDate.getTime()));
	  }

	  public void addDate(java.util.Date paramDate)
	  {
	    if (paramDate == null)
	      jm286(this.jm290 + 1, 93, null);
	    else
	      jm286(this.jm290 + 1, 93, new Timestamp(paramDate.getTime()));
	  }

	  public void setTimestamp(int paramInt, Timestamp paramTimestamp)
	  {
	    jm286(paramInt, 93, paramTimestamp);
	  }

	  public void setBigDecimal(int paramInt, BigDecimal paramBigDecimal)
	  {
	    jm286(paramInt, 8, new Double(paramBigDecimal.doubleValue()));
	  }

	  public void addBigDecimal(BigDecimal paramBigDecimal)
	  {
	    jm286(this.jm290 + 1, 8, new Double(paramBigDecimal.doubleValue()));
	  }

	  public void addParam(Object paramObject)
	  {
	    try
	    {
	      String str = jm0(paramObject.getClass());
	      if (str.equals("string"))
	      {
	        jm286(this.jm290 + 1, 12, paramObject);
	        return;
	      }
	      if (str.equals("date"))
	      {
	        jm286(this.jm290 + 1, 93, new Timestamp(((java.util.Date)paramObject).getTime()));
	        return;
	      }
	      if ((str.equals("integer")) || (str.equals("int")))
	      {
	        jm286(this.jm290 + 1, 2, Integer.valueOf(paramObject.toString()));
	        return;
	      }
	      if (str.equals("bigdecimal"))
	      {
	        jm286(this.jm290 + 1, 2, paramObject);
	        return;
	      }
	      jm286(this.jm290 + 1, 12, paramObject);
	    }
	    catch (Exception localException)
	    {
	      jm9.error(localException);
	    }
	  }

	  public void addParam(int paramInt, String paramString)
	  {
	    Object localObject = null;
	    switch (paramInt)
	    {
	    case 1:
	      localObject = paramString;
	      break;
	    case 2:
	      if ((paramString == null) || (paramString.length() < 1))
	        localObject = new BigDecimal(0.0D);
	      else
	        localObject = new BigDecimal(paramString);
	      break;
	    case 3:
	    case 4:
	    case 5:
	      if ((paramString == null) || (paramString.length() < 1))
	        localObject = new Integer(0);
	      else
	        localObject = Integer.valueOf(paramString);
	      break;
	    case 6:
	    case 7:
	    case 8:
	      if ((paramString == null) || (paramString.length() < 1))
	        localObject = new BigDecimal(0.0D);
	      else
	        localObject = new BigDecimal(paramString);
	      break;
	    case 12:
	      localObject = paramString;
	      break;
	    case 91:
	    case 92:
	    case 93:
	      if ((paramString != null) && (paramString.length() > 0))
	      {
	        XDate localDate = new XDate(paramString);
	        localObject = new Timestamp(localDate.getTime());
	      }
	      break;
	    case -1:
	      localObject = paramString;
	      break;
	    case -6:
	    case -5:
	      if ((paramString == null) || (paramString.length() < 1))
	        localObject = new Integer(0);
	      else
	        localObject = Integer.valueOf(paramString);
	      break;
	    default:
	      localObject = paramString;
	    }
	    jm286(this.jm290 + 1, paramInt, localObject);
	  }

	  public void addParam(int paramInt, Object paramObject)
	  {
	    if (paramObject != null)
	      if (((paramInt > 90) && (paramInt < 94)) || (paramInt == -100))
	      {
	        Class tmp29_26 = jm17;
	        if (tmp29_26 == null)
	        {
	          
	          try
	          {
	             DBSQL.jm17 = Class.forName("java.sql.Timestamp");
	          }
	          catch (ClassNotFoundException localClassNotFoundException1)
	          {
	            throw new NoClassDefFoundError(localClassNotFoundException1.getMessage());
	          }
	        }
	        if (!(paramObject.getClass().equals(tmp29_26)))
	          paramObject = new Timestamp(((java.util.Date)paramObject).getTime());
	      }
	      else if (2 == paramInt)
	      {
	        Class tmp94_91 = jm291;
	        if (tmp94_91 == null)
	        {
	          
	          try
	          {
	            DBSQL.jm291 = Class.forName("java.lang.String");
	          }
	          catch (ClassNotFoundException localClassNotFoundException2)
	          {
	            throw new NoClassDefFoundError(localClassNotFoundException2.getMessage());
	          }
	        }
	        if (paramObject.getClass().equals(tmp94_91))
	          paramObject = new BigDecimal(paramObject.toString());
	      }
	    jm286(this.jm290 + 1, paramInt, paramObject);
	  }

	  public void toPreparedStatement(PreparedStatement paramPreparedStatement)
	    throws SQLException
	  {
	    int i = 0;
	    String str;
	    try
	    {
	      for (i = 0; i < this.jm290; ++i)
	      {
	        Object localObject = this.jm288[i];
	        if (localObject == null)
	        {
	          if (this.jm289[i] == 0)
	            continue;
	          paramPreparedStatement.setNull(1 + i, this.jm289[i]);
	        }
	        else if ((12 == this.jm289[i]) && (localObject.toString().length() > 666))
	        {
	          str = localObject.toString();
	          paramPreparedStatement.setCharacterStream(1 + i, new StringReader(str), str.length());
	        }
	        else
	        {
	          paramPreparedStatement.setObject(1 + i, localObject, this.jm289[i]);
	        }
	      }
	    }
	    catch (SQLException localSQLException)
	    {
	      str = null;
	      if (this.jm288[i] == null)
	        str = "NULL";
	      else
	        str = this.jm288[i].getClass().getName();
	      jm9.error("PS setObject error idx=" + i + " type=" + this.jm289[i] + str);
	      throw localSQLException;
	    }
	    catch (Throwable localThrowable)
	    {
	      jm9.error("PS setObject error idx=" + i + " type=" + this.jm289[i], localThrowable);
	      throw new SQLException(localThrowable.toString());
	    }
	  }

	  public String toString()
	  {
	    StringBuffer localStringBuffer = new StringBuffer(this.jm287.length() + 32);
	    localStringBuffer.append(this.jm287);
	    if (this.jm290 > 0)
	    {
	      localStringBuffer.append("\t:\t");
	      for (int i = 0; i < this.jm290; ++i)
	      {
	        if (i > 0)
	          localStringBuffer.append(",");
	        localStringBuffer.append(this.jm289[i] + "|");
	        if (this.jm288[i] != null)
	          localStringBuffer.append(this.jm288[i].toString());
	        else
	          localStringBuffer.append("null");
	      }
	    }
	    return localStringBuffer.toString();
	  }

	  private String jm0(Class paramClass)
	  {
	    String str = paramClass.getName();
	    int i = str.lastIndexOf(46);
	    if (i >= 0)
	      str = str.substring(i + 1);
	    return str.toLowerCase();
	  }

	  public static void main(String[] paramArrayOfString)
	  {
	    DBSQL localDBSQL = new DBSQL();
	    localDBSQL.setSQL("SElect ");
	    localDBSQL.appendSQL(" cpcdoc where docid=?");
	    localDBSQL.setBigDecimal(1, new BigDecimal(1.0D));
	    localDBSQL.setDate(2, new java.util.Date());
	    localDBSQL.setInt(3, 333333333);
	    localDBSQL.setLong(4, 444444444L);
	    localDBSQL.setString(5, "555");
	    localDBSQL.setTimestamp(6, new Timestamp(System.currentTimeMillis()));
	    localDBSQL.setDate(7, null);
	    localDBSQL.addParam("88");
	    localDBSQL.setInt(9, 333333333);
	    DBWhereExp localDBWhereExp = new DBWhereExp("a.id=b.itemid");
	    localDBWhereExp.and(DBWhereExp.eq("a.pid", "99"));
	    localDBSQL.append(1, localDBWhereExp);
	    System.out.println(localDBSQL);
	  }
}
